//! 宏系统测试
//! 
//! 测试 DataForge 提供的各种宏功能

use dataforge::*;

#[test]
fn test_rand_num_macro() {
    // 测试整数范围
    for _ in 0..10 {
        let num = rand_num!(1, 100);
        println!("随机整数: {}", num);
        assert!(num >= 1 && num <= 100);
    }
    
    // 测试浮点数范围
    for _ in 0..10 {
        let num = rand_num!(1.0, 10.0);
        println!("随机浮点数: {}", num);
        assert!(num >= 1.0 && num <= 10.0);
    }
    
    // 测试单个值
    let single = 42;
    println!("单个值: {}", single);
    assert_eq!(single, 42);
}

#[test]
fn test_pattern_macro() {
    // 测试手机号模式
    let phone = pattern!("1[3-9]\\d{9}");
    println!("手机号模式: {}", phone);
    assert_eq!(phone.len(), 11);
    assert!(phone.starts_with('1'));
    
    // 测试邮箱模式
    let email = pattern!("[a-z]{5,10}@[a-z]{3,8}\\.(com|cn|org)");
    println!("邮箱模式: {}", email);
    assert!(email.contains('@'));
    assert!(email.contains('.'));
    
    // 测试身份证模式
    let id_card = pattern!("\\d{17}[0-9X]");
    println!("身份证模式: {}", id_card);
    assert_eq!(id_card.len(), 18);
}

#[test]
fn test_datetime_macro() {
    // 测试ISO格式
    let iso_date = datetime!("iso");
    println!("ISO日期: {}", iso_date);
    assert!(iso_date.contains('T'));
    
    // 测试时间戳
    let timestamp = datetime!("timestamp");
    println!("时间戳: {}", timestamp);
    assert!(timestamp.parse::<i64>().is_ok());
    
    // 测试自定义格式
    let custom_date = datetime!("%Y-%m-%d");
    println!("自定义日期: {}", custom_date);
    assert!(custom_date.matches('-').count() == 2);
}

#[test]
fn test_basic_data_generation() {
    // 使用生成器函数直接生成数据
    let data = serde_json::json!({
        "name": generators::name::zh_cn_fullname(),
        "age": rand_num!(18, 65),
        "email": generators::internet::email(),
        "phone": pattern!("1[3-9]\\d{9}"),
        "created_at": datetime!("iso"),
        "active": true
    });
    
    println!("生成的数据: {}", serde_json::to_string_pretty(&data).unwrap());
    
    assert!(data.is_object());
    assert!(data["name"].is_string());
    assert!(data["age"].is_number());
    assert!(data["email"].is_string());
    assert!(data["phone"].is_string());
    assert!(data["created_at"].is_string());
    assert!(data["active"].is_boolean());
    
    // 验证数据格式
    let age = data["age"].as_i64().unwrap();
    assert!(age >= 18 && age <= 65);
    
    let phone = data["phone"].as_str().unwrap();
    assert_eq!(phone.len(), 11);
    assert!(phone.starts_with('1'));
}

#[test]
fn test_nested_data_generation() {
    let data = serde_json::json!({
        "user": {
            "id": rand_num!(1, 1000),
            "profile": {
                "name": generators::name::zh_cn_fullname(),
                "phone": generators::number::phone_number_cn(),
                "address": {
                    "province": generators::address::zh_province(),
                    "city": "北京市",
                    "detail": generators::address::zh_address()
                }
            }
        },
        "metadata": {
            "created_at": generators::datetime::iso8601(),
            "version": "1.0"
        }
    });
    
    println!("嵌套数据: {}", serde_json::to_string_pretty(&data).unwrap());
    
    assert!(data["user"]["profile"]["name"].is_string());
    assert!(data["user"]["profile"]["address"]["province"].is_string());
    assert_eq!(data["user"]["profile"]["address"]["city"].as_str().unwrap(), "北京市");
    assert_eq!(data["metadata"]["version"].as_str().unwrap(), "1.0");
}

#[test]
fn test_array_generation() {
    let users: Vec<serde_json::Value> = (0..5).map(|_| serde_json::json!({
        "name": generators::name::zh_cn_fullname(),
        "age": rand_num!(20, 60)
    })).collect();
    
    let scores: Vec<i32> = (0..3).map(|_| rand_num!(60, 100)).collect();
    
    let data = serde_json::json!({
        "users": users,
        "tags": ["技术", "创新", "发展"],
        "scores": scores
    });
    
    println!("数组数据: {}", serde_json::to_string_pretty(&data).unwrap());
    
    assert!(data["users"].is_array());
    assert_eq!(data["users"].as_array().unwrap().len(), 5);
    assert!(data["tags"].is_array());
    assert_eq!(data["tags"].as_array().unwrap().len(), 3);
    assert!(data["scores"].is_array());
    assert_eq!(data["scores"].as_array().unwrap().len(), 3);
    
    // 验证生成的用户数据
    for user in data["users"].as_array().unwrap() {
        assert!(user["name"].is_string());
        assert!(user["age"].is_number());
        let age = user["age"].as_i64().unwrap();
        assert!(age >= 20 && age <= 60);
    }
}

#[test]
fn test_performance() {
    use std::time::Instant;
    
    let start = Instant::now();
    
    // 生成大量数据测试性能
    let mut results = Vec::new();
    for i in 0..1000 {
        let data = serde_json::json!({
            "id": i,
            "name": generators::name::zh_cn_fullname(),
            "phone": pattern!("1[3-9]\\d{9}"),
            "score": rand_num!(60, 100) as f64,
            "created_at": datetime!("iso")
        });
        results.push(data);
    }
    
    let duration = start.elapsed();
    println!("生成1000条记录耗时: {:?}", duration);
    
    assert_eq!(results.len(), 1000);
    assert!(duration.as_millis() < 5000); // 应该在5秒内完成
    
    // 验证第一条和最后一条数据
    assert_eq!(results[0]["id"].as_i64().unwrap(), 0);
    assert_eq!(results[999]["id"].as_i64().unwrap(), 999);
    
    for result in results.iter().take(5) {
        assert!(result["name"].is_string());
        assert!(result["phone"].is_string());
        assert!(result["score"].is_number());
        assert!(result["created_at"].is_string());
    }
}

#[test]
fn test_conditional_generation() {
    for i in 0..5 {
        let user_type = if i % 2 == 0 { "admin" } else { "user" };
        
        let permissions = if user_type == "admin" {
            vec!["read", "write", "delete"]
        } else {
            vec!["read"]
        };
        
        let level = if user_type == "admin" {
            rand_num!(8, 10)
        } else {
            rand_num!(1, 5)
        };
        
        let data = serde_json::json!({
            "id": i + 1,
            "type": user_type,
            "permissions": permissions,
            "profile": {
                "name": generators::name::zh_cn_fullname(),
                "level": level
            }
        });
        
        println!("用户数据 {}: {}", i + 1, serde_json::to_string_pretty(&data).unwrap());
        
        assert_eq!(data["type"].as_str().unwrap(), user_type);
        
        if user_type == "admin" {
            assert_eq!(data["permissions"].as_array().unwrap().len(), 3);
            let level = data["profile"]["level"].as_i64().unwrap();
            assert!(level >= 8 && level <= 10);
        } else {
            assert_eq!(data["permissions"].as_array().unwrap().len(), 1);
            let level = data["profile"]["level"].as_i64().unwrap();
            assert!(level >= 1 && level <= 5);
        }
    }
}